﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity;
using System.Linq.Dynamic;
namespace Data.Redis　//使用Redis实现查询
{
    public class Res_ItemRepository : EEE114RepositoryBase, IRepository<Res_Item>
    {
        public Entity.PagedList<Res_Item> GetRes_Item(int? cid, int? vid, int page)
        {
            redisClient.SelectDB(6); //可以分别存储在不同的数据库里，默认是0
            List<int> arr = new List<int>();
            List<int> arrVID = new List<int>();
            List<int> arrCID = new List<int>();
            List<object> list = new List<object>();
            if (cid.HasValue)
            {
                if (redisClient.ZRangeByScore("Res_Item_CID", cid.Value, cid.Value) != null)
                {
                    arrCID = redisClient.ZRangeByScore("Res_Item_CID", cid.Value, cid.Value).Cast<int>().ToList();
                    arr = arrCID;
                }
            }
            if (vid.HasValue)
            {
                if (redisClient.ZRangeByScore("Res_Item_VID", vid.Value, vid.Value) != null)
                {
                    arrVID = redisClient.ZRangeByScore("Res_Item_VID", vid.Value, vid.Value).Cast<int>().ToList();
                    arr = arrVID;
                }

            }
            if (cid.HasValue && vid.HasValue)
            {
                arr = arrVID.Intersect(arrCID).ToList();
            }

            if (arr != null && arr.Count > 0)
                foreach (int i in arr)
                {

                    try
                    {
                        list.AddRange(redisClient.ZRangeByScore("Res_Item", i, i));
                    }
                    catch (Exception)
                    {
                        continue;//出现异常，就逃过本次插入
                        //throw new Exception(i.ToString());
                    }

                }

            return new PagedList<Res_Item>(list.Cast<Res_Item>().DistinctBy(i => i.ID).AsQueryable(), page, 10);
        }

        #region IRepository<Res_Item> 成员

        public IQueryable<Res_Item> GetModel()
        {
            // redisClient.SelectDB(5); //可以分别存储在不同的数据库里，默认是0
            return redisClient.GetMembersOfSet("Res_Item").Cast<Res_Item>().OrderBy(i => i.ID).DistinctBy(j => j.ID).ToList().AsQueryable();
        }

        public void Insert(Res_Item entity)
        {
            redisClient.Add("Res_Item", entity.ID, entity);
        }

        public void Delete(Res_Item entity)
        {
            redisClient.Remove("Res_Item", entity);
        }

        public void Update(Res_Item entity)
        {
            throw new NotImplementedException();
        }



        #endregion

        #region IRepository<Res_Item> 成员


        public void Update(IList<Res_Item> list)
        {
            throw new NotImplementedException();
        }

        public void Insert(IList<Res_Item> list)
        {
            throw new NotImplementedException();
        }

        public Res_Item InsertGetIDENTITY(Res_Item entity)
        {
            throw new NotImplementedException();
        }

        public void Delete(IList<Res_Item> list)
        {
            throw new NotImplementedException();
        }

        public Res_Item Find(params object[] keyValues)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
